cd "Wilfahrt_APSR Replication Data"

* Import, clean, match Acled data
{
import delimited "Data/Calculated Output/AcledID.csv", varnames(1) clear 
keep acled_id grid_id

save "Data/Calculated Output/Base_DVs1.dta", replace

import delimited "Data/Outcome Variables/Conflict Data/1997-01-01-2023-03-01-East_Asia-Eastern_Africa-Middle_Africa-Southern_Africa-Western_Africa_MW.csv", varnames(1) clear
keep acled_id event_type fatalities interaction year

sort acled_id
save "Data/Calculated Output/Acled_Merge.dta", replace


use  "Data/Calculated Output/Base_DVs1.dta", clear
sort acled_id
merge acled_id using "Data/Calculated Output/Acled_Merge.dta"

gen Battles = 0
replace Battles = 1 if event_type == "Battles"
gen Riots = 0
replace Riots = 1 if event_type == "Riots"
gen Civ_Violence = 0
replace Civ_Violence = 1 if event_type == "Violence against civilians"

gen Battles_pre2013 = 0
replace Battles_pre2013 = 1 if event_type == "Battles" & year < 2014
gen Remote_pre2013 = 0
replace Remote_pre2013 = 1 if event_type == "Explosions/Remote violence" & year < 2014

gen State_Actor = 0
replace State_Actor = 1 if interaction == 12 | interaction == 13 | interaction == 14 | interaction == 23 | interaction == 33 | interaction == 34 

replace Battles_pre2013 = 0 if State_Actor == 0  & year < 2014
replace Remote_pre2013 = 0 if State_Actor == 0  & year < 2014

replace Battles = 0 if State_Actor == 0  
replace Remote = 0 if State_Actor == 0 

gen ViolenceSum = Battles_pre2013+Remote_pre2013
gen ViolenceSum_all = Battles+Remote

bysort grid_id year: gen N_yrs_conflict_Acled_pre2013 = 1 if ViolenceSum > 0 
bysort grid_id year N_yrs_conflict_Acled_pre2013: gen N2 = _n 
bysort grid_id year N_yrs_conflict_Acled_pre2013: egen maxN2 = max(N2)
replace N_yrs_conflict_Acled_pre2013 = 0 if N2 > 1

bysort grid_id year: gen N_yrs_conflict_Acled = 1 if ViolenceSum_all > 0 
bysort grid_id year N_yrs_conflict_Acled: gen N3 = _n 
bysort grid_id year N_yrs_conflict_Acled: egen maxN3 = max(N3)
replace N_yrs_conflict_Acled = 0 if N3 > 1

collapse (rawsum) N_yrs_conflict_Acled N_yrs_conflict_Acled_pre2013  Battles_pre2013 Remote_pre2013 Civ_Violence Battles Riots, by(grid_id)

gen Agg_Acled_All = Riots+Battles+Civ_Violence
gen ln_Agg_Acled_All = log(1+Agg_Acled_All)

replace Battles_pre2013 = 0 if Battles_pre2013 == .
replace Battles = 0 if Battles == .
replace Remote_pre2013 = 0 if Remote_pre2013 == .
replace N_yrs_conflict_Acled_pre2013 = 0 if N_yrs_conflict_Acled_pre2013 == .

gen Agg_Acled_pre2013 = Battles_pre2013+Remote_pre2013
gen Agg_Acled = Battles+Remote

gen ln_Agg_Acled_pre2013 = log(1+Agg_Acled_pre2013)
gen ln_N_yrs_conflict_Acled_pre2013 = log(1+N_yrs_conflict_Acled)
gen ln_Acled_Battles_pre2013 = log(1+Battles_pre2013)
gen ln_N_yrs_conflict_Acled = log(1+ln_N_yrs_conflict_Acled)
gen ln_Acled_Battles = log(1+Battles)
gen ln_Agg_Acled = log(1+Agg_Acled)

save "Data/Calculated Output/Acled_GRID_Final.dta", replace
}

* Import, clean, match GED data
{
import delimited "Data/Calculated Output/GEDID.csv", varnames(1) clear 
keep ged_id grid_id

save "Data/Calculated Output/Base_DVs2.dta", replace

import excel "Data/Outcome Variables/Conflict Data/GEDEvent_v23_1_MW.xlsx", sheet("GEDEvent_v23_1_MW") firstrow clear
keep id type_of_violence conflict_new_id year active_year where_prec year

rename id ged_id

keep if type_of_violence < 3
keep if where_prec < 6

gen N_Battles_State = 0
replace N_Battles_State = 1 if type_of_violence == 1 & active_year == 1
gen N_Battles_NonState = 0
replace N_Battles_NonState = 1 if type_of_violence == 2  & active_year == 1
gen N_Battles_Total = 0
replace N_Battles_Total = 1  & active_year == 1

gen N_Battles_State_pre2013 = 0
replace N_Battles_State_pre2013 = 1 if type_of_violence == 1 & active_year == 1 & year < 2014
gen N_Battles_NonState_pre2013 = 0
replace N_Battles_NonState_pre2013 = 1 if type_of_violence == 2  & active_year == 1 & year < 2014
gen N_Battles_Total_pre2013 = 0
replace N_Battles_Total_pre2013 = 1  & active_year == 1 & year < 2014

sort ged_id
save "Data/Calculated Output/GED_Merge.dta", replace

use  "Data/Calculated Output/Base_DVs2.dta", clear
sort ged_id
merge ged_id using "Data/Calculated Output/GED_Merge.dta"
drop if _merge == 2

collapse (first) year active_year type_of_violence conflict_new_id where_prec N_Battles_State N_Battles_NonState N_Battles_Total N_Battles_State_pre2013 N_Battles_NonState_pre2013 N_Battles_Total_pre2013, by (ged_id grid_id)

bysort grid_id conflict_new_id: gen N_Conflicts = 1
bysort grid_id conflict_new_id: gen N = _n
replace N_Conflicts = 0 if N > 1

bysort grid_id conflict_new_id: gen N_Conflicts_pre2013 = 1 if year < 2014
bysort grid_id conflict_new_id: gen N2 = _n if year < 2014
replace N_Conflicts_pre2013 = 0 if N2 > 1 & year < 2014

bysort grid_id year: gen N_yrs_conflict = 1 if active_year == 1
bysort grid_id year: gen N3= _n
bysort grid_id year: replace N_yrs_conflict = 0 if N3 > 1

bysort grid_id year: gen N_yrs_conflict_pre2013 = 1 if active_year == 1 
bysort grid_id year: gen N4 = _n 
replace N_yrs_conflict_pre2013 = 0 if N4 > 1 
replace N_yrs_conflict_pre2013 = . if  year > 2013

bysort grid_id conflict_new_id: gen N_StateConflicts = 1 if active_year == 1 & type_of_violence == 1
bysort grid_id conflict_new_id: gen N5 = _n
replace N_StateConflicts = 0 if N5 > 1

bysort grid_id conflict_new_id: gen N_StateConflicts_pre2013 = 1 if active_year == 1 & type_of_violence == 1 
bysort grid_id conflict_new_id: gen N6 = _n 
replace N_StateConflicts_pre2013 = 0 if N6 > 1 

bysort grid_id year: gen N_yrs_Stateconflict = 1 if active_year == 1 & type_of_violence == 1
bysort grid_id year: gen N7 = _n
replace N_yrs_Stateconflict = 0 if N7 > 1

bysort grid_id year: gen N_yrs_Stateconflict_pre2013  = 1 if active_year == 1 & type_of_violence == 1 
bysort grid_id year: gen N8 = _n 
replace N_yrs_Stateconflict_pre2013  = 0 if N8 > 1 
replace N_yrs_Stateconflict_pre2013 = . if  year > 2013

collapse (rawsum)   N_yrs_Stateconflict_pre2013 N_Conflicts_pre2013 N_StateConflicts_pre2013 N_yrs_conflict_pre2013 N_Battles_Total_pre2013 N_Battles_State_pre2013 N_Battles_NonState_pre2013  N_Conflicts N_yrs_conflict N_Battles_Total N_Battles_State N_Battles_NonState, by(grid_id)

gen ln_N_yrs_conflict_pre2013 = log(1+N_yrs_conflict_pre2013)
gen ln_N_Battles_Total_pre2013 = log(1+N_Battles_Total_pre2013)
gen ln_N_Conflicts_pre2013 = log(1+N_Conflicts_pre2013)
gen ln_N_Battles_State_pre2013 = log(1+N_Battles_State_pre2013)
gen ln_N_StateConflicts_pre2013 = log(1+N_StateConflicts_pre2013)
gen ln_N_yrs_Stateconflict_pre2013 = log(1+N_yrs_Stateconflict_pre2013)

gen ln_N_Battles_State = log(1+N_Battles_State)
gen ln_N_StateConflicts = log(1+N_StateConflicts)
gen ln_N_yrs_Stateconflict = log(1+N_yrs_Stateconflict)

save "Data/Calculated Output/GED_GRID_Final.dta", replace
}

* Import, clean, match Meta Wealth Data
{
import delimited "Data/Calculated Output/D_Gov_HealthCenter.csv", varnames(1) clear 

collapse (mean) rwi, by(grid_id)

sort grid_id
save "Data/Calculated Output/MetaWealth_GRID_Final.dta", replace
}

*** cleaning for controls
{
import delimited "Data/Calculated Output/Country_Grid_Split.csv", varnames(1) clear
keep grid_id countryx countryarea_km2x grid_country_area_km  

rename countryx country

replace countryarea_km2x = subinstr(countryarea_km2x," [km^2]", "", .)
gen country_area_km2 = real(countryarea_km2x)

replace grid_country_area_km = subinstr(grid_country_area_km," [km^2]", "", .)
gen grid_country_area_km2 = real(grid_country_area_km)

bysort grid_id: egen max_area = max(grid_country_area_km_2)
keep if grid_country_area_km_2 == max_area

bysort country: egen country_area_km_2 = max(country_area_km2)
drop country_area_km2
rename country_area_km_2 country_area_km2

gen Colonizer = ""
replace Colonizer = "Spain" if country == "Equatorial Guinea" 
replace Colonizer = "Italy" if country == "Eritrea" | country == "Somalia"
replace Colonizer = "Belgium" if country == "Rwanda" | country == "Burundi" | country == "Democratic Republic of Congo"
replace Colonizer = "Portugal" if country == "Angola" | country == "Guinea-Bissau" | country == "Mozambique"
replace Colonizer = "France" if country == "Benin" | country == "Burkina Faso" | country == "Cameroon" | country == "Central African Republic" | country == "Chad"  | country == "Congo-Brazzaville"  | country == "Cote d`Ivoire"  | country == "Djibouti"  | country == "Gabon"  | country == "Guinea"  | country == "Madagascar"  | country == "Mali"  | country == "Mauritania"  | country == "Niger"  | country == "Senegal"  | country == "Togo"
replace Colonizer = "UK" if country == "Botswana" | country == "Gambia" | country == "Ghana" | country == "Kenya"  | country == "Lesotho"  | country == "Malawi"  | country == "Namibia"  | country == "Nigeria"  | country == "Sierra Leone"  | country == "Sudan" | country == "South Sudan" | country == "Swaziland"  | country == "Tanzania"  | country == "Uganda"  | country == "Zambia"  | country == "Zimbabwe" | country == "South Africa"

keep grid_id country Colonizer country_area_km2
sort grid_id
save "Data/Calculated Output/Grid_Country.dta", replace

import delimited "Data/Calculated Output/Grid_StateReach.csv", varnames(1) clear
drop id
sort grid_id
save "Data/Calculated Output/Grid_StateReach.dta", replace

*calc % country territory covered by a polity
import delimited "Data/Calculated Output/Country_Grid_Split.csv", varnames(1) clear
keep grid_id countryx countryarea_km2x grid_country_area_km polity_8hr polity_8hr_country_area_km2

rename countryx country
rename polity_8hr_country_area_km2 polity_8hr_country_area_km2_2

replace countryarea_km2x = subinstr(countryarea_km2x," [km^2]", "", .)
gen country_area_km2 = real(countryarea_km2x)

egen tag = tag(country country_area_km2)
egen country_area_km2_2 = sum(tag * country_area_km2), by(country)

replace grid_country_area_km = subinstr(grid_country_area_km," [km^2]", "", .)
gen grid_country_area_km2 = real(grid_country_area_km)

replace polity_8hr_country_area_km2_2 = subinstr(polity_8hr_country_area_km2_2," [km^2]", "", .)
gen polity_8hr_country_area_km2 = real(polity_8hr_country_area_km2_2)

bysort grid_id: egen max_area = max(grid_country_area_km2)
keep if grid_country_area_km2 == max_area

bysort country: egen country_area_km2_3 = max(country_area_km2_2)
drop country_area_km2
rename country_area_km2_3 country_area_km2

encode country, gen(Country_N)

bysort polity_8hr: egen Max_country = mode(Country_N)
replace Max_country = . if polity_8hr == ""

bysort grid_id polity_8hr: egen min_polity_area = min(polity_8hr_country_area_km2)
gen Mark_min = 1 if polity_8hr_country_area_km2 == min_polity_area
bysort grid_id: egen max_polity_area = max(polity_8hr_country_area_km2)
gen Mark_max = 1 if polity_8hr_country_area_km2 == max_polity_area

replace polity_8hr_country_area_km2 = . if Mark_min == 1 & Max_country != Country_N
replace polity_8hr_country_area_km2 = . if Mark_max == 1 & Max_country != Country_N

drop Mark_min Mark_max min_polity_area max_polity_area

bysort grid_id polity_8hr: egen min_polity_area = min(polity_8hr_country_area_km2)
gen Mark_min = 1 if polity_8hr_country_area_km2 == min_polity_area
bysort grid_id: egen max_polity_area = max(polity_8hr_country_area_km2)
gen Mark_max = 1 if polity_8hr_country_area_km2 == max_polity_area

replace polity_8hr_country_area_km2 = . if Mark_min == 1 & Max_country != Country_N
replace polity_8hr_country_area_km2 = . if Mark_max == 1 & Max_country != Country_N

drop max_polity_area
bysort grid_id: egen max_polity_area = max(polity_8hr_country_area_km2)
keep if polity_8hr_country_area_km2 == max_polity_area

replace polity_8hr_country_area_km2 = 1232.344 if polity_8hr == "Badibu"
replace polity_8hr_country_area_km2 = 1060.890 if polity_8hr == "Djimara"
replace polity_8hr_country_area_km2 = 308.785 if polity_8hr == "Eropina"
replace polity_8hr_country_area_km2 = 398.284 if polity_8hr == "Niamina"
replace polity_8hr_country_area_km2 = 1256.434 if polity_8hr == "Tomani"
replace polity_8hr_country_area_km2 = 294.985 if polity_8hr == "Jokadu"

bysort Country: egen Max_area = max(country_area_km2)
drop country_area_km2
rename Max_area country_area_km2

gen Polity_Perc_Nation_8hr = polity_8hr_country_area_km2/country_area_km2

keep  grid_id Polity_Perc_Nation_8hr polity_8hr_country_area_km2
rename polity_8hr Atlas_8hr
sort grid_id
save "Data/Calculated Output/Polity_Country_Area.dta", replace


import delimited "Data/Calculated Output/Dist_Border_10km.csv", varnames(1) clear
keep inputid distance
gen d_border_km = distance/1000
rename inputid grid_id
collapse (min) d_border_km, by(grid_id)
drop distance 
sort grid_id
save "Data/Calculated Output/Dist_Borders_10km.dta", replace

import delimited "Data/Calculated Output/Dist_Capitals_10km.csv", varnames(1) clear
rename inputid grid_id
rename targetid country_capital
gen dist_capital_km = distance/1000

sort grid_id
merge grid_id using  "Data/Calculated Output/Dist_Borders_10km.dta",

replace country_capital = "Swaziland" if country_capital == "eSwatini"
replace country_capital = "Gambia" if country_capital == "The Gambia"
replace country_capital = "Congo-Brazzaville" if country_capital == "Congo (Brazzaville)"
replace country_capital = "Democratic Republic of Congo" if country_capital == "Congo (Kinshasa)"
replace country_capital = "Cote d`Ivoire" if country_capital == "Ivory Coast"
replace country_capital = "Somalia" if country_capital == "Somaliland"
replace country_capital = "Guinea-Bissau" if country_capital == "Guinea Bissau"

keep grid_id country_capital dist_capital_km d_border_km
sort grid_id
merge grid_id using "Data/Calculated Output/Grid_Country.dta"

bysort grid_id: gen Match = 0
bysort grid_id: replace Match = 1 if country == country_capital
bysort grid_id: replace Match = 1 if country == "South Sudan" & country_capital == "Sudan"

bysort grid_id: gen Match2 = 1 if country == "South Sudan" & country_capital == "Sudan"

drop if Match == 0

gen dist_capital_km_2 = .
replace dist_capital_km_2 = dist_capital_km if Match2 == 1
bysort grid_id: egen dist_capital_km_pre2011 = max(dist_capital_km_2) 
drop if Match2 == 1

bysort grid_id: egen min_dist = min(dist_capital_km)
keep if min_dist == dist_capital_km

replace dist_capital_km_pre2011 = dist_capital_km if dist_capital_km_pre2011 == .

keep grid_id dist_capital_km dist_capital_km_pre2011 country Colonizer country_area_km  d_border_km
sort grid_id
save "Data/Calculated Output/Grid_Country_Capital_Dist.dta", replace


import delimited "Data/Calculated Output/Distance to Coast.csv", varnames(1) clear
rename inputid grid_id
gen D_Coast_km = distance/1000
keep  grid_id D_Coast_km
sort grid_id
save "Data/Calculated Output/Grid_DCoast.dta", replace

import delimited "Data/Calculated Output/CovariatesWhite.csv", varnames(1) clear 
drop id
sort grid_id
save "Data/Calculated Output/CovariatesWhite.dta", replace

import delimited "Data/Calculated Output/Covariates.csv", varnames(1) clear 

rename centroids1 grid_longitude
rename centroids2 grid_latitude
rename grid_perch20x grid_perc_h20
rename grid_idx grid_id
rename nightlight_20132_meanx nightlight_20132_mean
rename grid_area_kmx grid_area_km
rename water_area_km2x water_area_km2

replace water_area_km2 = 0 if water_area_km2 == .
replace grid_perc_h20 = 0 if grid_perc_h20 == .
replace mt_binary_mean = 0 if mt_binary_mean == .

gen Rugged_SlopeFinal =(rugged_slopettl/rugged_cellareattl)/1000
gen Rugged_RuggednessFinal =(rugged_trittl/rugged_cellareattl)/100000

gen Diamonds = 0
replace Diamonds = 1 if diamonds != .
gen Gems = 0
replace Gems = 1 if gems != .
gen Silver = 0
replace Silver = 1 if silver != .
gen Iron = 0
replace Iron = 1 if iron != .
gen Copper = 0
replace Copper = 1 if copper != .
gen Tin = 0
replace Tin = 1 if tin != .
gen Gold = 0
replace Gold = 1 if gold != .

gen NaturalResources = 0
replace NaturalResource = (Diamonds+Gems+Silver+Iron+Copper+Tin+Gold)

drop diamonds gems silver gold tin copper iron nightlight_20132_meany grid_perch20y water_area_km2y grid_area_kmy

rename img_prob_p EWI_p_poor
rename img_prob_l EWI_p_lowincome
rename img_prob_u EWI_p_upperincome
rename img_prob_r EWI_p_rich
rename estimated_ EWI_Index

rename atlantic_1 atlantic_1500
rename atlantic_11 atlantic_1600
rename atlantic_12 atlantic_1700
rename atlantic_13 atlantic_1800
rename atlantic_t atlantic_ttl

collapse (first)  popcount_2010ad natcap2000 natcap1966 regcap1966 regcap2000 natcap2015 regcap2015  EWI_p_poor EWI_p_lowincome EWI_p_upperincome EWI_p_rich EWI_Index indian_ttl atlantic_ttl indian_18 indian_17 indian_16 indian_15 atlantic_1800 atlantic_1700 atlantic_1600 atlantic_1500  Diamonds Gems Silver Iron Copper Tin Gold NaturalResources popdensity_1000_base popdensity_1800_base popdensity_1000_lower popdensity_1800_lower popdensity_1000_upper popdensity_1800_upper cropland_1000_base cropland_1800_base cropland_1000_lower cropland_1800_lower cropland_1000_upper cropland_1800_upper Rugged_SlopeFinal Rugged_RuggednessFinal grid_longitude grid_latitude grid_area_km water_area_km2 grid_perc_h20  nightlight_20132_mean  mt_binary_mean grid_mttype malaria_2000   elevation landsuitability land_use precipitation rugged_cellareattl rugged_slopettl rugged_trittl cellarea, by(grid_id)

sort grid_id
merge grid_id using "Data/Calculated Output/CovariatesWhite.dta"

rename gridcode Whites_id
rename white_area whiteeco_area

bysort grid_id whiteeco_area Whites_id: gen N = _n
keep if N == 1

hhi whiteeco_area, by(grid_id)
rename hhi_whiteeco_area Eco_Diversity

replace Eco_Diversity = . if whiteeco_area == .

collapse (first) whiteeco_area Eco_Diversity popcount_2010ad natcap2000 natcap1966 regcap1966 regcap2000 natcap2015 regcap2015  EWI_p_poor EWI_p_lowincome EWI_p_upperincome EWI_p_rich EWI_Index indian_ttl atlantic_ttl indian_18 indian_17 indian_16 indian_15 atlantic_1800 atlantic_1700 atlantic_1600 atlantic_1500  Diamonds Gems Silver Iron Copper Tin Gold NaturalResources popdensity_1000_base popdensity_1800_base popdensity_1000_lower popdensity_1800_lower popdensity_1000_upper popdensity_1800_upper cropland_1000_base cropland_1800_base cropland_1000_lower cropland_1800_lower cropland_1000_upper cropland_1800_upper Rugged_SlopeFinal Rugged_RuggednessFinal grid_longitude grid_latitude grid_area_km water_area_km2 grid_perc_h20  nightlight_20132_mean  mt_binary_mean grid_mttype malaria_2000   elevation landsuitability land_use precipitation rugged_cellareattl rugged_slopettl rugged_trittl cellarea, by(grid_id)

sort grid_id
merge grid_id using "Data/Calculated Output/Grid_Country_Capital_Dist.dta"
drop _merge

sort grid_id
merge grid_id using "Data/Calculated Output/Grid_DCoast.dta"
drop _merge

sort grid_id
merge grid_id using "Data/Calculated Output/Grid_StateReach.dta"
drop _merge

sort grid_id
save "Data/Calculated Output/Controls_GRID_Final.dta", replace
}

*** CLIPPED KINGDOMS
*Atlas Main
{
import delimited "Data/Calculated Output/Atlas_Main.csv", varnames(1) clear 
keep atlas_6hr_area_km_intersect atlas_8hr_area_km_intersect atlas_10hr_area_km_intersect atlas_6hr_area_km atlas_8hr_area_km atlas_10hr_area_km grid_area_km  water_area_km2 polity_6hr polity_8hr polity_10hr atlas_6hr_area_km atlas_8hr_area_km atlas_10hr_area_km grid_id

*6hr
*assign grids that see two polities to the later polity
gen Ndebele = 1 if polity_6hr == "Ndebele"
bysort grid_id: egen Ndebele_Any = max(Ndebele)
gen Macina = 1 if polity_6hr == "Macina"
bysort grid_id: egen Macina_Any = max(Macina)
gen Segou = 1 if polity_6hr == "Segou"
bysort grid_id: egen Segou_Any = max(Segou)
gen Kaarta = 1 if polity_6hr == "Kaarta"
bysort grid_id: egen Kaarta_Any = max(Kaarta)
gen Harar = 1 if polity_6hr == "Harar"
bysort grid_id: egen Harar_Any = max(Harar)
gen Dawro = 1 if polity_6hr == "Dawro"
bysort grid_id: egen Dawro_Any = max(Dawro)
gen Gera = 1 if polity_6hr == "Gera"
bysort grid_id: egen Gera_Any = max(Gera)
gen Gomma = 1 if polity_6hr == "Gomma"
bysort grid_id: egen Gomma_Any = max(Gomma)
gen Gumma = 1 if polity_6hr == "Gumma"
bysort grid_id: egen Gumma_Any = max(Gumma)
gen Janjera = 1 if polity_6hr == "Janjera"
bysort grid_id: egen Janjera_Any = max(Janjera)
gen Jimma = 1 if polity_6hr == "Jimma"
bysort grid_id: egen Jimma_Any = max(Jimma)
gen Kaffa = 1 if polity_6hr == "Kaffa"
bysort grid_id: egen Kaffa_Any = max(Kaffa)
gen Konta = 1 if polity_6hr == "Konta"
bysort grid_id: egen Konta_Any = max(Konta)
gen LeqaNaqamt = 1 if polity_6hr == "Leqa Naqamte"
bysort grid_id: egen LeqaNaqamt_Any = max(LeqaNaqamt)
gen LeqaQellem = 1 if polity_6hr == "Leqa Qellem"
bysort grid_id: egen LeqaQellem_Any = max(LeqaQellem)
gen LimmuEnnar = 1 if polity_6hr == "Limmu Enarea"
bysort grid_id: egen LimmuEnnar_Any = max(LimmuEnnar)
gen Shewa = 1 if polity_6hr == "Shewa"
bysort grid_id: egen Shewa_Any = max(Shewa)
gen Wolayta = 1 if polity_6hr == "Wolayta"
bysort grid_id: egen Wolayta_Any = max(Wolayta)
gen Anfillo = 1 if polity_6hr == "Anfillo"
bysort grid_id: egen Anfillo_Any = max(Anfillo)

gen Double6 = 1 if polity_6hr == "Rozvi" & Ndebele_Any == 1
replace Double6 = 1 if polity_6hr == "Umarian" & Macina_Any == 1
replace Double6 = 1 if polity_6hr == "Umarian" & Segou_Any == 1
replace Double6 = 1 if polity_6hr == "Samory" & Segou_Any == 1
replace Double6 = 1 if polity_6hr == "Umarian" & Kaarta_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Harar_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Dawro_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Gera_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Gomma_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Gumma_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Janjera_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Jimma_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Konta_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & LeqaNaqamt_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & LeqaQellem_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Shewa_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Wolayta_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Kaffa_Any == 1
replace Double6 = 1 if polity_6hr == "Menelik's Ethiopia" & Anfillo_Any == 1

replace atlas_6hr_area_km = 0 if Double6 == 1
replace atlas_6hr_area_km_intersect = 0 if Double6 == 1
bysort grid_id: egen max_Polity = max(atlas_6hr_area_km_intersect) if Double6 != 1
gen Polity_Clip_6hr = ""
gen Polity_Clip_6hr_area = .
replace Polity_Clip_6hr = polity_6hr if max_Polity == atlas_6hr_area_km_intersect
replace Polity_Clip_6hr_area = atlas_6hr_area_km_intersect if max_Polity == atlas_6hr_area_km_intersect 

bysort grid_id (Polity_Clip_6hr): replace Polity_Clip_6hr = Polity_Clip_6hr[_N] if missing(Polity_Clip_6hr) 
bysort grid_id (Polity_Clip_6hr_area): replace Polity_Clip_6hr_area = Polity_Clip_6hr_area[1] if missing(Polity_Clip_6hr_area) 

gen Polity_6hr_ttl_area_km = atlas_6hr_area_km if Polity_Clip_6hr == polity_6hr
bysort grid_id (Polity_6hr_ttl_area_km): replace Polity_6hr_ttl_area_km = Polity_6hr_ttl_area_km[1] if missing(Polity_6hr_ttl_area_km) 

drop  max_Polity atlas_6hr_area_km_intersect Ndebele Ndebele_Any  Macina Macina_Any Segou Segou_Any Kaarta Kaarta_Any Harar Harar_Any Dawro Dawro_Any Gera Gera_Any Gomma Gomma_Any Gumma Gumma_Any Janjera Janjera_Any Jimma Jimma_Any Kaffa Kaffa_Any Konta Konta_Any LeqaNaqamt LeqaNaqamt_Any LeqaQellem LeqaQellem_Any LimmuEnnar LimmuEnnar_Any Shewa Shewa_Any Wolayta Wolayta_Any Anfillo Anfillo_Any

*8hr
gen Ndebele = 1 if polity_8hr == "Ndebele"
bysort grid_id: egen Ndebele_Any = max(Ndebele)
gen Macina = 1 if polity_8hr == "Macina"
bysort grid_id: egen Macina_Any = max(Macina)
gen Segou = 1 if polity_8hr == "Segou"
bysort grid_id: egen Segou_Any = max(Segou)
gen Kaarta = 1 if polity_8hr == "Kaarta"
bysort grid_id: egen Kaarta_Any = max(Kaarta)
gen Harar = 1 if polity_8hr == "Harar"
bysort grid_id: egen Harar_Any = max(Harar)
gen Dawro = 1 if polity_8hr == "Dawro"
bysort grid_id: egen Dawro_Any = max(Dawro)
gen Gera = 1 if polity_8hr == "Gera"
bysort grid_id: egen Gera_Any = max(Gera)
gen Gomma = 1 if polity_8hr == "Gomma"
bysort grid_id: egen Gomma_Any = max(Gomma)
gen Gumma = 1 if polity_8hr == "Gumma"
bysort grid_id: egen Gumma_Any = max(Gumma)
gen Janjera = 1 if polity_8hr == "Janjera"
bysort grid_id: egen Janjera_Any = max(Janjera)
gen Jimma = 1 if polity_8hr == "Jimma"
bysort grid_id: egen Jimma_Any = max(Jimma)
gen Kaffa = 1 if polity_8hr == "Kaffa"
bysort grid_id: egen Kaffa_Any = max(Kaffa)
gen Konta = 1 if polity_8hr == "Konta"
bysort grid_id: egen Konta_Any = max(Konta)
gen LeqaNaqamt = 1 if polity_8hr == "LeqaNaqamt"
bysort grid_id: egen LeqaNaqamt_Any = max(LeqaNaqamt)
gen LeqaQellem = 1 if polity_8hr == "LeqaQellem"
bysort grid_id: egen LeqaQellem_Any = max(LeqaQellem)
gen LimmuEnnar = 1 if polity_8hr == "LimmuEnnar"
bysort grid_id: egen LimmuEnnar_Any = max(LimmuEnnar)
gen Shewa = 1 if polity_8hr == "Shewa"
bysort grid_id: egen Shewa_Any = max(Shewa)
gen Wolayta = 1 if polity_8hr == "Wolayta"
bysort grid_id: egen Wolayta_Any = max(Wolayta)
gen Anfillo = 1 if polity_8hr == "Anfillo"
bysort grid_id: egen Anfillo_Any = max(Anfillo)

gen Double8 = 1 if polity_8hr == "Rozvi" & Ndebele_Any == 1
replace Double8 = 1 if polity_8hr == "Umarian" & Macina_Any == 1
replace Double8 = 1 if polity_8hr == "Umarian" & Segou_Any == 1
replace Double8 = 1 if polity_8hr == "Samory" & Segou_Any == 1
replace Double8 = 1 if polity_8hr == "Umarian" & Kaarta_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Harar_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Dawro_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Gera_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Gomma_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Gumma_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Janjera_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Jimma_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Konta_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & LeqaNaqamt_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & LeqaQellem_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Shewa_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Wolayta_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Kaffa_Any == 1
replace Double8 = 1 if polity_8hr == "Menelik's Ethiopia" & Anfillo_Any == 1

replace atlas_8hr_area_km = 0 if Double8 == 1
replace atlas_8hr_area_km_intersect = 0 if Double8 == 1
bysort grid_id: egen max_Polity = max(atlas_8hr_area_km_intersect) if Double8 != 1

gen Polity_Clip_8hr = ""
gen Polity_Clip_8hr_area = .
replace Polity_Clip_8hr = polity_8hr if max_Polity == atlas_8hr_area_km_intersect
replace Polity_Clip_8hr_area = atlas_8hr_area_km_intersect if max_Polity == atlas_8hr_area_km_intersect 

bysort grid_id (Polity_Clip_8hr): replace Polity_Clip_8hr = Polity_Clip_8hr[_N] if missing(Polity_Clip_8hr) 
bysort grid_id (Polity_Clip_8hr_area): replace Polity_Clip_8hr_area = Polity_Clip_8hr_area[1] if missing(Polity_Clip_8hr_area) 

gen Polity_8hr_ttl_area_km = atlas_8hr_area_km if Polity_Clip_8hr == polity_8hr
bysort grid_id (Polity_8hr_ttl_area_km): replace Polity_8hr_ttl_area_km = Polity_8hr_ttl_area_km[1] if missing(Polity_8hr_ttl_area_km) 

drop atlas_8hr_area_km_intersect  max_Polity Ndebele Ndebele_Any Double8 Macina Macina_Any Segou Segou_Any Kaarta Kaarta_Any Harar Harar_Any Dawro Dawro_Any Gera Gera_Any Gomma Gomma_Any Gumma Gumma_Any Janjera Janjera_Any Jimma Jimma_Any Kaffa Kaffa_Any Konta Konta_Any LeqaNaqamt LeqaNaqamt_Any LeqaQellem LeqaQellem_Any LimmuEnnar LimmuEnnar_Any Shewa Shewa_Any Wolayta Wolayta_Any Anfillo Anfillo_Any

*10hr
gen Ndebele = 1 if polity_10hr == "Ndebele"
bysort grid_id: egen Ndebele_Any = max(Ndebele)
gen Macina = 1 if polity_10hr == "Macina"
bysort grid_id: egen Macina_Any = max(Macina)
gen Segou = 1 if polity_10hr == "Segou"
bysort grid_id: egen Segou_Any = max(Segou)
gen Kaarta = 1 if polity_10hr == "Kaarta"
bysort grid_id: egen Kaarta_Any = max(Kaarta)
gen Harar = 1 if polity_10hr == "Harar"
bysort grid_id: egen Harar_Any = max(Harar)
gen Dawro = 1 if polity_10hr == "Dawro"
bysort grid_id: egen Dawro_Any = max(Dawro)
gen Gera = 1 if polity_10hr == "Gera"
bysort grid_id: egen Gera_Any = max(Gera)
gen Gomma = 1 if polity_10hr == "Gomma"
bysort grid_id: egen Gomma_Any = max(Gomma)
gen Gumma = 1 if polity_10hr == "Gumma"
bysort grid_id: egen Gumma_Any = max(Gumma)
gen Janjera = 1 if polity_10hr == "Janjera"
bysort grid_id: egen Janjera_Any = max(Janjera)
gen Jimma = 1 if polity_10hr == "Jimma"
bysort grid_id: egen Jimma_Any = max(Jimma)
gen Kaffa = 1 if polity_10hr == "Kaffa"
bysort grid_id: egen Kaffa_Any = max(Kaffa)
gen Konta = 1 if polity_10hr == "Konta"
bysort grid_id: egen Konta_Any = max(Konta)
gen LeqaNaqamt = 1 if polity_10hr == "LeqaNaqamt"
bysort grid_id: egen LeqaNaqamt_Any = max(LeqaNaqamt)
gen LeqaQellem = 1 if polity_10hr == "LeqaQellem"
bysort grid_id: egen LeqaQellem_Any = max(LeqaQellem)
gen LimmuEnnar = 1 if polity_10hr == "LimmuEnnar"
bysort grid_id: egen LimmuEnnar_Any = max(LimmuEnnar)
gen Shewa = 1 if polity_10hr == "Shewa"
bysort grid_id: egen Shewa_Any = max(Shewa)
gen Wolayta = 1 if polity_10hr == "Wolayta"
bysort grid_id: egen Wolayta_Any = max(Wolayta)
gen Anfillo = 1 if polity_10hr == "Anfillo"
bysort grid_id: egen Anfillo_Any = max(Anfillo)

gen Double10 = 1 if polity_10hr == "Rozvi" & Ndebele_Any == 1
replace Double10 = 1 if polity_10hr == "Umarian" & Macina_Any == 1
replace Double10 = 1 if polity_10hr == "Umarian" & Segou_Any == 1
replace Double10 = 1 if polity_10hr == "Samory" & Segou_Any == 1
replace Double10 = 1 if polity_10hr == "Umarian" & Kaarta_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Harar_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Dawro_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Gera_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Gomma_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Gumma_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Janjera_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Jimma_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Konta_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & LeqaNaqamt_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & LeqaQellem_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Shewa_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Wolayta_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Kaffa_Any == 1
replace Double10 = 1 if polity_10hr == "Menelik's Ethiopia" & Anfillo_Any == 1

replace atlas_10hr_area_km = 0 if Double10 == 1
replace atlas_10hr_area_km_intersect = 0 if Double10 == 1
bysort grid_id: egen max_Polity = max(atlas_10hr_area_km_intersect) if Double10 != 1

gen Polity_Clip_10hr = ""
gen Polity_Clip_10hr_area = .
replace Polity_Clip_10hr = polity_10hr if max_Polity == atlas_10hr_area_km_intersect
replace Polity_Clip_10hr_area = atlas_10hr_area_km_intersect if max_Polity == atlas_10hr_area_km_intersect 

bysort grid_id (Polity_Clip_10hr): replace Polity_Clip_10hr = Polity_Clip_10hr[_N] if missing(Polity_Clip_10hr) 
bysort grid_id (Polity_Clip_10hr_area): replace Polity_Clip_10hr_area = Polity_Clip_10hr_area[1] if missing(Polity_Clip_10hr_area) 

gen Polity_10hr_ttl_area_km = atlas_10hr_area_km if Polity_Clip_10hr == polity_10hr
bysort grid_id (Polity_10hr_ttl_area_km): replace Polity_10hr_ttl_area_km = Polity_10hr_ttl_area_km[1] if missing(Polity_10hr_ttl_area_km) 

* collapse all
collapse (first) Polity_Clip_8hr_area Polity_Clip_10hr_area Polity_Clip_6hr_area Polity_10hr_ttl_area_km Polity_8hr_ttl_area_km Polity_6hr_ttl_area_km water_area_km2 grid_area_km  polity_6hr polity_8hr polity_10hr  , by(grid_id)

gen polity_6hr_2 = polity_6hr
gen polity_8hr_2 = polity_8hr
gen polity_10hr_2 = polity_10hr

gen Water_Area = water_area_km2/grid_area_km
replace Water_Area = 0 if water_area_km2 == .

gen mark = 0
replace mark = 1 if Water_Area > 0.75

gen Overlap6hr = Polity_Clip_6hr_area/grid_area_km
replace polity_6hr = "" if Overlap6hr < 0.25
replace polity_6hr = polity_6hr_2 if mark == 1

gen Overlap8hr = Polity_Clip_8hr_area/grid_area_km
replace polity_8hr = "" if Overlap8hr < 0.25
replace polity_8hr = polity_8hr_2 if mark == 1

gen Overlap10hr = Polity_Clip_10hr_area/grid_area_km
replace polity_10hr = "" if Overlap10hr < 0.25
replace polity_10hr = polity_10hr_2 if mark == 1

drop mark Overlap6hr Overlap8hr Overlap10hr polity_6hr_2 polity_8hr_2 polity_10hr_2  Polity_Clip_8hr_area Polity_Clip_10hr_area Polity_Clip_6hr_area 

rename polity_6hr Atlas_6hr
rename polity_8hr Atlas_8hr
rename polity_10hr Atlas_10hr

sort grid_id
save "Data/Calculated Output/Atlas_GRID_Final.dta", replace
}

* 1880 Clip
{
import delimited "Data/Calculated Output/Atlas_1880.csv", varnames(1) clear 
keep grid_area_km atlas_1880_6hr_area_km atlas_1880_8hr_area_km atlas_1880_10hr_area_km water_area_km2 atlas_1880_6hr atlas_1880_6hr_area_km_intersect atlas_1880_8hr atlas_1880_8hr_area_km_intersect atlas_1880_10hr atlas_1880_10hr_area_km_intersec grid_id

*6hr
bysort grid_id: egen max_Polity = max(atlas_1880_6hr_area_km_intersect)
bysort grid_id: egen max_Polity_ttl = max(atlas_1880_6hr_area_km)

gen Polity_1880_6hr = ""
gen Polity_1880_6hr_area = .
gen Polity_1880_6hr_ttl_area_km = .
replace Polity_1880_6hr = atlas_1880_6hr if max_Polity == atlas_1880_6hr_area_km_intersect
replace Polity_1880_6hr_area = atlas_1880_6hr_area_km_intersect if max_Polity == atlas_1880_6hr_area_km_intersect 
replace Polity_1880_6hr_ttl_area_km = atlas_1880_6hr_area_km if max_Polity_ttl == atlas_1880_6hr_area_km 

bysort grid_id (Polity_1880_6hr): replace Polity_1880_6hr = Polity_1880_6hr[_N] if missing(Polity_1880_6hr) 
bysort grid_id (Polity_1880_6hr_area): replace Polity_1880_6hr_area = Polity_1880_6hr_area[1] if missing(Polity_1880_6hr_area) 
bysort grid_id (Polity_1880_6hr_ttl_area_km): replace Polity_1880_6hr_ttl_area_km = Polity_1880_6hr_ttl_area_km[1] if missing(Polity_1880_6hr_ttl_area_km) 

drop max_Polity max_Polity_ttl

*8hr
bysort grid_id: egen max_Polity = max(atlas_1880_8hr_area_km_intersect)
bysort grid_id: egen max_Polity_ttl = max(atlas_1880_8hr_area_km)

gen Polity_1880_8hr = ""
gen Polity_1880_8hr_area = .
gen Polity_1880_8hr_ttl_area_km = .
replace Polity_1880_8hr = atlas_1880_8hr if max_Polity == atlas_1880_8hr_area_km_intersect
replace Polity_1880_8hr_area = atlas_1880_8hr_area_km_intersect if max_Polity == atlas_1880_8hr_area_km_intersect 
replace Polity_1880_8hr_ttl_area_km = atlas_1880_8hr_area_km if max_Polity_ttl == atlas_1880_8hr_area_km 

bysort grid_id (Polity_1880_8hr): replace Polity_1880_8hr = Polity_1880_8hr[_N] if missing(Polity_1880_8hr) 
bysort grid_id (Polity_1880_8hr): replace Polity_1880_8hr = Polity_1880_8hr[1] if missing(Polity_1880_8hr) 
bysort grid_id (Polity_1880_8hr_ttl_area_km): replace Polity_1880_8hr_ttl_area_km = Polity_1880_8hr_ttl_area_km[1] if missing(Polity_1880_8hr_ttl_area_km) 

drop max_Polity max_Polity_ttl

*10hr
bysort grid_id: egen max_Polity = max(atlas_1880_10hr_area_km_intersec) 
bysort grid_id: egen max_Polity_ttl = max(atlas_1880_10hr_area_km)

gen Polity_1880_10hr = ""
gen Polity_1880_10hr_area = .
gen Polity_1880_10hr_ttl_area_km = .
replace Polity_1880_10hr = atlas_1880_10hr if max_Polity == atlas_1880_10hr_area_km_intersec
replace Polity_1880_10hr_area = atlas_1880_10hr_area_km_intersec if max_Polity == atlas_1880_10hr_area_km_intersec 
replace Polity_1880_10hr_ttl_area_km = atlas_1880_10hr_area_km if max_Polity_ttl == atlas_1880_10hr_area_km 

bysort grid_id (Polity_1880_10hr): replace Polity_1880_10hr = Polity_1880_10hr[_N] if missing(Polity_1880_10hr) 
bysort grid_id (Polity_1880_10hr_area): replace Polity_1880_10hr_area = Polity_1880_10hr_area[1] if missing(Polity_1880_10hr_area) 
bysort grid_id (Polity_1880_10hr_ttl_area_km): replace Polity_1880_10hr_ttl_area_km = Polity_1880_10hr_ttl_area_km[1] if missing(Polity_1880_10hr_ttl_area_km) 

collapse (first) Polity_1880_8hr_ttl_area_km Polity_1880_10hr_ttl_area_km Polity_1880_6hr_ttl_area_km water_area_km2 grid_area_km Polity_1880_10hr  Polity_1880_6hr Polity_1880_8hr Polity_1880_6hr_area  Polity_1880_8hr_area Polity_1880_10hr_area, by(grid_id)

gen atlas_1880_6hr_2 = Polity_1880_6hr
gen atlas_1880_8hr_2 = Polity_1880_8hr
gen atlas_1880_10hr_2 = Polity_1880_10hr

gen Water_Area = water_area_km2/grid_area_km
replace Water_Area = 0 if water_area_km2 == .

gen mark = 0
replace mark = 1 if Water_Area > 0.75

gen Overlap6hr = Polity_1880_6hr_area/grid_area_km
replace Polity_1880_6hr = "" if Overlap6hr < 0.25
replace Polity_1880_6hr = atlas_1880_6hr_2 if mark == 1

gen Overlap8hr = Polity_1880_8hr_area/grid_area_km
replace Polity_1880_8hr = "" if Overlap8hr < 0.25
replace Polity_1880_8hr = atlas_1880_8hr_2 if mark == 1

gen Overlap10hr = Polity_1880_10hr_area/grid_area_km
replace Polity_1880_10hr = "" if Overlap10hr < 0.25
replace Polity_1880_10hr = atlas_1880_10hr_2 if mark == 1

drop Overlap6hr Overlap8hr Overlap10hr atlas_1880_6hr_2 atlas_1880_8hr_2 atlas_1880_10hr_2 Polity_1880_6hr_area Polity_1880_8hr_area Polity_1880_10hr_area

rename Polity_1880_6hr Atlas_1880_6hr
rename Polity_1880_8hr Atlas_1880_8hr
rename Polity_1880_10hr Atlas_1880_10hr

sort grid_id
save "Data/Calculated Output/Atlas_1880_GRID_Final.dta", replace
}

* what should be intersection (25%) and what should be total areas

* Composite Clip
{
import delimited "Data/Calculated Output/Atlas_Composite.csv", varnames(1) clear 
keep grid_area_km water_area_km2 atlas_comp_6hr  atlas_comp_8hr atlas_comp_10hr atlas_comp_6hr_area_km atlas_comp_6hr_area_km_intersect atlas_comp_8hr_area_km_intersect atlas_comp_10hr_area_km_intersec atlas_comp_8hr_area_km atlas_comp_10hr_area_km grid_id

gen Double6 = 1 if  atlas_comp_6hr == "Rozvi" | atlas_comp_6hr ==  "Menelik's Ethiopia" | atlas_comp_6hr == "Umarian" 
gen Double8 = 1 if atlas_comp_8hr == "Rozvi" | atlas_comp_8hr ==  "Menelik's Ethiopia" | atlas_comp_8hr == "Umarian" 
gen Double10 = 1 if  atlas_comp_10hr == "Rozvi" | atlas_comp_10hr ==  "Menelik's Ethiopia" | atlas_comp_10hr == "Umarian"

*6hr
bysort grid_id: gen N_Polity = _n if atlas_comp_6hr_area_km_intersect != .
gen mark6 = 1 if N_Polity > 1 & Double6 == 1
replace atlas_comp_6hr_area_km_intersect = 0 if mark6 == 1
bysort grid_id: egen max_Polity = max(atlas_comp_6hr_area_km_intersect) if N_Polity != 1
bysort grid_id: egen max_Polity_ttl = max(atlas_comp_6hr_area_km) if N_Polity != 1

gen Polity_Clip_Comp_6hr = ""
gen Polity_Clip_Comp_6hr_area = .
gen Polity_Comp_6hr_ttl_area_km = .
replace Polity_Clip_Comp_6hr = atlas_comp_6hr if max_Polity == atlas_comp_6hr_area_km_intersect
replace Polity_Clip_Comp_6hr_area = atlas_comp_6hr_area_km_intersect if max_Polity == atlas_comp_6hr_area_km_intersect 
replace Polity_Comp_6hr_ttl_area_km = atlas_comp_6hr_area_km if max_Polity_ttl == atlas_comp_6hr_area_km 

bysort grid_id (Polity_Clip_Comp_6hr): replace Polity_Clip_Comp_6hr = Polity_Clip_Comp_6hr[_N] if missing(Polity_Clip_Comp_6hr) 
bysort grid_id (Polity_Clip_Comp_6hr_area): replace Polity_Clip_Comp_6hr_area = Polity_Clip_Comp_6hr_area[1] if missing(Polity_Clip_Comp_6hr_area) 
bysort grid_id (Polity_Comp_6hr_ttl_area_km): replace Polity_Comp_6hr_ttl_area_km = Polity_Comp_6hr_ttl_area_km[1] if missing(Polity_Comp_6hr_ttl_area_km) 

drop max_Polity N_Polity max_Polity_ttl

*8hr
bysort grid_id: gen N_Polity = _n if atlas_comp_8hr_area_km_intersect != .
gen mark8 = 1 if N_Polity > 1 & Double8 == 1
replace atlas_comp_8hr_area_km_intersect = 0 if mark8 == 1
bysort grid_id: egen max_Polity = max(atlas_comp_8hr_area_km_intersect) if N_Polity != 1
bysort grid_id: egen max_Polity_ttl = max(atlas_comp_8hr_area_km) if N_Polity != 1

gen Polity_Clip_Comp_8hr = ""
gen Polity_Clip_Comp_8hr_area = .
gen Polity_Comp_8hr_ttl_area_km = .
replace Polity_Clip_Comp_8hr = atlas_comp_8hr if max_Polity == atlas_comp_8hr_area_km_intersect
replace Polity_Clip_Comp_8hr_area = atlas_comp_8hr_area_km_intersect if max_Polity == atlas_comp_8hr_area_km_intersect 
replace Polity_Comp_8hr_ttl_area_km = atlas_comp_8hr_area_km if max_Polity_ttl == atlas_comp_8hr_area_km 

bysort grid_id (Polity_Clip_Comp_8hr): replace Polity_Clip_Comp_8hr = Polity_Clip_Comp_8hr[_N] if missing(Polity_Clip_Comp_8hr) 
bysort grid_id (Polity_Clip_Comp_8hr_area): replace Polity_Clip_Comp_8hr_area = Polity_Clip_Comp_8hr_area[1] if missing(Polity_Clip_Comp_8hr_area) 
bysort grid_id (Polity_Comp_8hr_ttl_area_km): replace Polity_Comp_8hr_ttl_area_km = Polity_Comp_8hr_ttl_area_km[1] if missing(Polity_Comp_8hr_ttl_area_km) 

drop max_Polity N_Polity max_Polity_ttl

*10hr
bysort grid_id: gen N_Polity = _n if atlas_comp_10hr_area_km_intersec != .
gen mark10 = 1 if N_Polity > 1 & Double10 == 1
replace atlas_comp_10hr_area_km_intersec = 0 if mark10 == 1
bysort grid_id: egen max_Polity = max(atlas_comp_10hr_area_km_intersec) if N_Polity != 1
bysort grid_id: egen max_Polity_ttl = max(atlas_comp_10hr_area_km) if N_Polity != 1

gen Polity_Clip_Comp_10hr = ""
gen Polity_Clip_Comp_10hr_area = .
gen Polity_Comp_10hr_ttl_area_km = .
replace Polity_Clip_Comp_10hr = atlas_comp_10hr if max_Polity == atlas_comp_10hr_area_km_intersec
replace Polity_Clip_Comp_10hr_area = atlas_comp_10hr_area_km_intersec if max_Polity == atlas_comp_10hr_area_km_intersec 
replace Polity_Comp_10hr_ttl_area_km = atlas_comp_10hr_area_km if max_Polity_ttl == atlas_comp_10hr_area_km 

bysort grid_id (Polity_Clip_Comp_10hr): replace Polity_Clip_Comp_10hr = Polity_Clip_Comp_10hr[_N] if missing(Polity_Clip_Comp_10hr) 
bysort grid_id (Polity_Clip_Comp_10hr_area): replace Polity_Clip_Comp_10hr_area = Polity_Clip_Comp_10hr_area[1] if missing(Polity_Clip_Comp_10hr_area) 
bysort grid_id (Polity_Comp_10hr_ttl_area_km): replace Polity_Comp_10hr_ttl_area_km = Polity_Comp_10hr_ttl_area_km[1] if missing(Polity_Comp_10hr_ttl_area_km) 

collapse (first) water_area_km2 Polity_Comp_10hr_ttl_area_km Polity_Comp_8hr_ttl_area_km Polity_Comp_6hr_ttl_area_km Polity_Clip_Comp_10hr_area Polity_Clip_Comp_8hr_area grid_area_km Polity_Clip_Comp_10hr Polity_Clip_Comp_8hr Polity_Clip_Comp_6hr Polity_Clip_Comp_6hr_area, by(grid_id)

replace Polity_Clip_Comp_6hr = "Sokoto Emirates" if  Polity_Clip_Comp_6hr == "Sokoto"

gen Polity_Clip_6hr_2 = Polity_Clip_Comp_6hr
gen Polity_Clip_8hr_2 = Polity_Clip_Comp_8hr
gen Polity_Clip_10hr_2 = Polity_Clip_Comp_10hr

gen Water_Area = water_area_km2/grid_area_km
replace Water_Area = 0 if water_area_km2 == .

gen mark = 0
replace mark = 1 if Water_Area > 0.75

gen Overlap6hr = Polity_Clip_Comp_6hr_area/grid_area_km
replace Polity_Clip_Comp_6hr = "" if Overlap6hr < 0.25
replace Polity_Clip_Comp_6hr = Polity_Clip_6hr_2 if mark == 1

gen Overlap8hr = Polity_Clip_Comp_8hr_area/grid_area_km
replace Polity_Clip_Comp_8hr = "" if Overlap8hr < 0.25
replace Polity_Clip_Comp_8hr = Polity_Clip_8hr_2 if mark == 1

gen Overlap10hr = Polity_Clip_Comp_10hr_area/grid_area_km
replace Polity_Clip_Comp_10hr = "" if Overlap10hr < 0.25
replace Polity_Clip_Comp_10hr = Polity_Clip_10hr_2 if mark == 1

drop Overlap6hr Overlap8hr Overlap10hr Polity_Clip_6hr_2 Polity_Clip_8hr_2 Polity_Clip_10hr_2

rename Polity_Clip_Comp_6hr Atlas_Composite_6hr
rename Polity_Clip_Comp_8hr Atlas_Composite_8hr
rename Polity_Clip_Comp_10hr Atlas_Composite_10hr

sort grid_id
save "Data/Calculated Output/Atlas_Comp_GRID_Final.dta", replace
}

* Composite 1880 Clip
{
import delimited "Data/Calculated Output/Atlas_1880_Composite.csv", varnames(1) clear 
keep grid_area_km water_area_km2 atlas_comp_1880_6hr_area_km_inte atlas_comp_1880_8hr_area_km_inte atlas_comp_1880_10hr_area_km_int   atlas_comp_1880_6hr atlas_comp_1880_8hr atlas_comp_1880_10hr    grid_id

rename atlas_comp_1880_6hr_area_km_inte Atlas_Comp_1880_6hr_area_km
rename atlas_comp_1880_8hr_area_km_inte Atlas_Comp_1880_8hr_area_km
rename atlas_comp_1880_10hr_area_km_int Atlas_Comp_1880_10hr_area_km

gen Double6 = 1 if atlas_comp_1880_6hr ==  "LateEthiop" | atlas_comp_1880_6hr == "Umarian" 
gen Double8 = 1 if atlas_comp_1880_8hr ==  "LateEthiop" | atlas_comp_1880_8hr == "Umarian" 
gen Double10 = 1 if  atlas_comp_1880_10hr ==  "LateEthiop" | atlas_comp_1880_10hr == "Umarian"


*6hr
bysort grid_id: gen N_Polity = _n if Atlas_Comp_1880_6hr_area_km != .
gen mark6 = 1 if N_Polity > 1 & Double6 == 1
replace Atlas_Comp_1880_6hr_area_km = 0 if mark6 == 1
bysort grid_id: egen max_Polity = max(Atlas_Comp_1880_6hr_area_km) if N_Polity != 1

gen Polity_Clip_Comp_1880_6hr = ""
gen Polity_Clip_Comp_1880_6hr_area = .
replace Polity_Clip_Comp_1880_6hr = atlas_comp_1880_6hr if max_Polity == Atlas_Comp_1880_6hr_area_km
replace Polity_Clip_Comp_1880_6hr_area = Atlas_Comp_1880_6hr_area_km if max_Polity == Atlas_Comp_1880_6hr_area_km 

bysort grid_id (Polity_Clip_Comp_1880_6hr): replace Polity_Clip_Comp_1880_6hr = Polity_Clip_Comp_1880_6hr[_N] if missing(Polity_Clip_Comp_1880_6hr) 
bysort grid_id (Polity_Clip_Comp_1880_6hr_area): replace Polity_Clip_Comp_1880_6hr_area = Polity_Clip_Comp_1880_6hr_area[1] if missing(Polity_Clip_Comp_1880_6hr_area) 

drop max_Polity N_Polity

*8hr
bysort grid_id: gen N_Polity = _n if Atlas_Comp_1880_8hr_area_km != .
gen mark8 = 1 if N_Polity > 1 & Double8 == 1
replace Atlas_Comp_1880_8hr_area_km = 0 if mark8 == 1
bysort grid_id: egen max_Polity = max(Atlas_Comp_1880_8hr_area_km) if N_Polity != 1

gen Polity_Clip_Comp_1880_8hr = ""
gen Polity_Clip_Comp_1880_8hr_area = .
replace Polity_Clip_Comp_1880_8hr = atlas_comp_1880_8hr if max_Polity == Atlas_Comp_1880_8hr_area_km
replace Polity_Clip_Comp_1880_8hr_area = Atlas_Comp_1880_8hr_area_km if max_Polity == Atlas_Comp_1880_8hr_area_km 

bysort grid_id (Polity_Clip_Comp_1880_8hr): replace Polity_Clip_Comp_1880_8hr = Polity_Clip_Comp_1880_8hr[_N] if missing(Polity_Clip_Comp_1880_8hr) 
bysort grid_id (Polity_Clip_Comp_1880_8hr_area): replace Polity_Clip_Comp_1880_8hr_area = Polity_Clip_Comp_1880_8hr_area[1] if missing(Polity_Clip_Comp_1880_8hr_area) 

drop max_Polity N_Polity

*10hr
bysort grid_id: gen N_Polity = _n if Atlas_Comp_1880_10hr_area_km != .
gen mark10 = 1 if N_Polity > 1 & Double10 == 1
replace Atlas_Comp_1880_10hr_area_km = 0 if mark10 == 1
bysort grid_id: egen max_Polity = max(Atlas_Comp_1880_10hr_area_km) if N_Polity != 1


gen Polity_Clip_Comp_1880_10hr = ""
gen Polity_Clip_Comp_1880_10hr_area = .
replace Polity_Clip_Comp_1880_10hr = atlas_comp_1880_10hr if max_Polity == Atlas_Comp_1880_10hr_area_km
replace Polity_Clip_Comp_1880_10hr_area = Atlas_Comp_1880_10hr_area_km if max_Polity == Atlas_Comp_1880_10hr_area_km 

bysort grid_id (Polity_Clip_Comp_1880_10hr): replace Polity_Clip_Comp_1880_10hr = Polity_Clip_Comp_1880_10hr[_N] if missing(Polity_Clip_Comp_1880_10hr) 
bysort grid_id (Polity_Clip_Comp_1880_10hr_area): replace Polity_Clip_Comp_1880_10hr_area = Polity_Clip_Comp_1880_10hr_area[1] if missing(Polity_Clip_Comp_1880_10hr_area) 

collapse (first) water_area_km2 Polity_Clip_Comp_1880_10hr_area Polity_Clip_Comp_1880_8hr_area grid_area_km Polity_Clip_Comp_1880_10hr Polity_Clip_Comp_1880_8hr Polity_Clip_Comp_1880_6hr Polity_Clip_Comp_1880_6hr_area, by(grid_id)

gen Polity_Clip_6hr_2 = Polity_Clip_Comp_1880_6hr
gen Polity_Clip_8hr_2 = Polity_Clip_Comp_1880_8hr
gen Polity_Clip_10hr_2 = Polity_Clip_Comp_1880_10hr

gen Water_Area = water_area_km2/grid_area_km
replace Water_Area = 0 if water_area_km2 == .

gen mark = 0
replace mark = 1 if Water_Area > 0.75

gen Overlap6hr = Polity_Clip_Comp_1880_6hr_area/grid_area_km
replace Polity_Clip_Comp_1880_6hr = "" if Overlap6hr < 0.25
replace Polity_Clip_Comp_1880_6hr = Polity_Clip_6hr_2 if mark == 1

gen Overlap8hr = Polity_Clip_Comp_1880_8hr_area/grid_area_km
replace Polity_Clip_Comp_1880_8hr = "" if Overlap8hr < 0.25
replace Polity_Clip_Comp_1880_8hr = Polity_Clip_8hr_2 if mark == 1

gen Overlap10hr = Polity_Clip_Comp_1880_10hr_area/grid_area_km
replace Polity_Clip_Comp_1880_10hr = "" if Overlap10hr < 0.25
replace Polity_Clip_Comp_1880_10hr = Polity_Clip_10hr_2 if mark == 1

drop Overlap6hr Overlap8hr Overlap10hr Polity_Clip_6hr_2 Polity_Clip_8hr_2 Polity_Clip_10hr_2

rename Polity_Clip_Comp_1880_6hr Atlas_Composite_1880_6hr
rename Polity_Clip_Comp_1880_8hr Atlas_Composite_1880_8hr
rename Polity_Clip_Comp_1880_10hr Atlas_Composite_1880_10hr

sort grid_id
save "Data/Calculated Output/Atlas_Comp_1880_GRID_Final.dta", replace
}

* Contested territory - 0/1 if > 50% of grid contested
{
import delimited "Data/Calculated Output/Contested_Territory.csv", varnames(1) clear 
keep grid_id polity_contested_comp_1880_8hr contested_composite_1880_8hr_are contested_1880_8hr_ar polity_contested_1880_8hr grid_area_km

collapse (max) polity_contested_comp_1880_8hr contested_composite_1880_8hr_are contested_1880_8hr_ar polity_contested_1880_8hr grid_area_km, by(grid_id)

rename contested_1880_8hr_ar contested_1880_8hr_area_km
rename contested_composite_1880_8hr_are contested_comp_1880_8hr_area_km

gen Perc_Contested_1880 = contested_1880_8hr_area_km/grid_area_km
replace Perc_Contested_1880 = 1 if Perc_Contested_1880 > 1
gen Perc_Contested_Comp_1880 = contested_comp_1880_8hr_area_km/grid_area_km
replace Perc_Contested_Comp_1880 = 1 if Perc_Contested_Comp_1880 > 1

gen Contested_1880_8hr = 0
replace Contested_1880_8hr = 1 if Perc_Contested_1880 > .49

gen Contested_Comp_1880_8hr = 0
replace Contested_Comp_1880_8hr = 1 if Perc_Contested_Comp_1880 > .49

keep Contested_1880_8hr Contested_Comp_1880_8hr grid_id
sort grid_id
save "Data/Calculated Output/Contested_GRID_Final.dta", replace
}

* Uncertainty Estimates
{
import delimited "Data/Calculated Output/Uncertainty_Estimates.csv", varnames(1) clear 
collapse uncertaintyest_max_area_km_inter grid_area_km, by(grid_id polity_uncertaintymax)

* first triage to assign mixed but not complete grids to largest kingdom
bysort grid_id: egen Max_Max = max(uncertaintyest_max_area_km_inter)
bysort grid_id: gen Perc_Max_Max = Max_Max/grid_area_km
bysort grid_id: gen Perc_Max = uncertaintyest_max_area_km_inter/grid_area_km
bysort grid_id: egen Perc_Max2 = max(Perc_Max)
gen mark = 1 if Perc_Max_Max == Perc_Max 

gen Final_polity_uncertaintymax = ""
replace Final_polity_uncertaintymax = polity_uncertaintymax if mark == 1 & Perc_Max_Max < 1.05

* then assign those only covered by one state
bysort grid_id: gen N2 = _n 
bysort grid_id: egen max_N = max(N2)

replace Final_polity_uncertaintymax = polity_uncertaintymax if max_N == 1

gen N3 = 1 if polity_uncertaintymax == "Rozvi" | polity_uncertaintymax ==  "LateEthiop" | polity_uncertaintymax == "Umarian" 

replace Final_polity_uncertaintymax = polity_uncertaintymax if N3 == 1

replace Final_polity_uncertaintymax= polity_uncertaintymax if Final_polity_uncertaintymax == ""

collapse (first) Final_polity_uncertaintymax, by(grid_id)

sort grid_id
save "Data/Calculated Output/Uncertainty_MAX_Final.dta", replace

** MIN
import delimited "Data/Calculated Output/Uncertainty_Estimates.csv", varnames(1) clear 
collapse uncertaintyest_min_area_km_inter grid_area_km, by(grid_id polity_uncertaintymin)

* first triage to assign mixed but not complete grids to largest kingdom
bysort grid_id: egen Max_Min = max(uncertaintyest_min_area_km_inter)
bysort grid_id: gen Perc_Max_Min = Max_Min/grid_area_km
bysort grid_id: gen Perc_Min = uncertaintyest_min_area_km_inter/grid_area_km
bysort grid_id: egen Perc_Min2 = max(Perc_Min)
gen mark = 1 if Perc_Max_Min == Perc_Min 

gen Final_polity_uncertaintymin = ""
replace Final_polity_uncertaintymin = polity_uncertaintymin if mark == 1 & Perc_Max_Min < 1.05

* then assign those only covered by one state
bysort grid_id: gen N2 = _n 
bysort grid_id: egen max_N = max(N2)

replace Final_polity_uncertaintymin = polity_uncertaintymin if max_N == 1

gen N3 = 1 if polity_uncertaintymin == "Rozvi" | polity_uncertaintymin ==  "LateEthiop" | polity_uncertaintymin == "Umarian" 

replace Final_polity_uncertaintymin = polity_uncertaintymin if N3 == 1

replace Final_polity_uncertaintymin= polity_uncertaintymin if Final_polity_uncertaintymin == ""

collapse (first) Final_polity_uncertaintymin, by(grid_id)

sort grid_id
save "Data/Calculated Output/Uncertainty_MIN_Final.dta", replace
}

* Import, clean, match 1 and 2 degree grid fixed effects
{
import delimited "Data/Calculated Output/Grid_1_degree_MERGE.csv", varnames(1) clear 

bysort grid_id: egen max_grid = max(grid_1deg_area_km)

keep if grid_1deg_area_km == max_grid

keep grid_id grid1dg_id
sort grid_id
save "Data/Calculated Output/Grid_1_degree_MERGE.dta", replace

import delimited "Data/Calculated Output/Grid_2_degree_MERGE.csv", varnames(1) clear 

bysort grid_id: egen max_grid = max(grid_2deg_area_km)

keep if grid_2deg_area_km == max_grid

keep grid_id grid2dg_id
sort grid_id
save "Data/Calculated Output/Grid_2_degree_MERGE.dta", replace


import delimited "Data/Calculated Output/Grid_0_5_degree_MERGE.csv", varnames(1) clear 

bysort grid_id: egen max_grid = max(grid_0_5deg_area_km)
keep if grid_0_5deg_area_km == max_grid

keep grid_id grid0_5_id
sort grid_id
save "Data/Calculated Output/Grid_0_5_degree_MERGE.dta", replace


import delimited "Data/Calculated Output/Grid_3_degree_MERGE.csv", varnames(1) clear 

bysort grid_id: egen max_grid = max(grid_3deg_area_km)
keep if grid_3deg_area_km == max_grid

rename grid_3dg_i grid_3dg_id

keep grid_id grid_3dg_id
sort grid_id
save "Data/Calculated Output/Grid_3_degree_MERGE.dta", replace

import delimited "Data/Calculated Output/Grid_5_degree_MERGE.csv", varnames(1) clear 

bysort grid_id: egen max_grid = max(grid_5deg_area_km)
keep if grid_5deg_area_km == max_grid

rename grid_5dg_i grid_5dg_id

keep grid_id grid_5dg_id
sort grid_id
save "Data/Calculated Output/Grid_5_degree_MERGE.dta", replace
}


**** MERGE ABOVE TOGETHER
{
use "Data/Calculated Output/Controls_GRID_Final.dta", clear
sort grid_id
merge grid_id using "Data/Calculated Output/Atlas_GRID_Final.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Atlas_1880_GRID_Final.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Atlas_Comp_GRID_Final.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Atlas_Comp_1880_GRID_Final.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Contested_GRID_Final.dta"
drop _merge
sort grid_id
merge grid_id using  "Data/Calculated Output/Uncertainty_MIN_Final.dta"
drop _merge
sort grid_id
merge grid_id using  "Data/Calculated Output/Uncertainty_MAX_Final.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Grid_1_degree_MERGE.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Grid_2_degree_MERGE.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Grid_0_5_degree_MERGE.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Grid_3_degree_MERGE.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Grid_5_degree_MERGE.dta"
drop _merge
save "Data/Calculated Output/Atlas_Combined_Final.dta", replace
}


** MERGE IN STATE DATA
{
import excel "Data/Atlas of 19th c Polities/Polity_Data.xlsx", firstrow clear
keep Polity ConquerElite JudicialAutonomy Councilors KingmakersElectors Ideology Ideology Slavery Caste Persistence Stability Elite_Structure N_Sources Economy_Trade Economy_Pastoralism Economy_Agriculture Trade_FoodStuffs_Livestock Trade_Minerals Trade_EnslavedPersons Trade_Rtes Elite_Ethnicity Polity_Age Islamic
sort Polity
save "Data/Calculated Output/Atlas_Final.dta", replace

import delimited using "Data/Calculated Output/Nightlight_1992.csv", clear
keep grid_id nightlight_1992_mean
sort grid_id
save "Data/Calculated Output/Nightlight_1992.dta", replace


import delimited using "Data/Calculated Output/Covariates_Precipitation.csv",  varnames(1) clear
keep grid_id popdensity_0ad precipitation
rename precipitation CHIRPS_Prec_mm
sort grid_id
save "Data/Covariates_precipitation.dta", replace


use "Data/Calculated Output/Atlas_Combined_Final.dta", clear
rename Atlas_6hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_6hr
rename Elite_Structure LineageStruc_6hr
rename N_Sources N_Sources_6hr
rename Trade_FoodStuffs_Livestock Trade_FoodStuffs_Livestock_6hr
rename Trade_Minerals Trade_Minerals_6hr
rename Trade_EnslavedPersons Trade_EnslavedPersons_6hr
rename Economy_Agriculture Economy_Agriculture_6hr
rename Economy_Pastoralism Economy_Pastoralism_6hr
rename Economy_Trade Economy_Trade_6hr
rename Caste Caste_6hr
rename Slavery Slavery_6hr
rename Polity_Age Polity_Age_6hr
rename Persistence Persistence_6hr
rename Stability Stability_6hr
rename Trade_Rtes Trade_Rtes_6hr
rename Councilors Councilors_6hr
rename KingmakersElectors KingmakersElectors_6hr
rename Elite_Ethnicity Elite_Ethnicity_6hr
rename Polity Atlas_6hr

rename Atlas_8hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_8hr
rename Elite_Structure LineageStruc_8hr
rename N_Sources N_Sources_8hr
rename Trade_FoodStuffs_Livestock Trade_FoodStuffs_Livestock_8hr
rename Trade_Minerals Trade_Minerals_8hr
rename Trade_EnslavedPersons Trade_EnslavedPersons_8hr
rename Economy_Agriculture Economy_Agriculture_8hr
rename Economy_Pastoralism Economy_Pastoralism_8hr
rename Economy_Trade Economy_Trade_8hr
rename Caste Caste_8hr
rename Slavery Slavery_8hr
rename Polity_Age Polity_Age_8hr
rename Persistence Persistence_8hr
rename Stability Stability_8hr
rename Trade_Rtes Trade_Rtes_8hr
rename Councilors Councilors_8hr
rename KingmakersElectors KingmakersElectors_8hr
rename Elite_Ethnicity Elite_Ethnicity_8hr
rename ConquerElite ConquerElite_8hr
rename Polity Atlas_8hr
rename JudicialAutonomy JudicialAutonomy_8hr

rename Atlas_10hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_10hr
rename Elite_Structure LineageStruc_10hr
rename N_Sources N_Sources_10hr
rename Trade_FoodStuffs_Livestock Trade_FoodStuffs_Livestock_10hr
rename Trade_Minerals Trade_Minerals_10hr
rename Trade_EnslavedPersons Trade_EnslavedPersons_10hr
rename Economy_Agriculture Economy_Agriculture_10hr
rename Economy_Pastoralism Economy_Pastoralism_10hr
rename Economy_Trade Economy_Trade_10hr
rename Caste Caste_10hr
rename Slavery Slavery_10hr
rename Polity_Age Polity_Age_10hr
rename Persistence Persistence_10hr
rename Stability Stability_10hr
rename Trade_Rtes Trade_Rtes_10hr
rename Councilors Councilors_10hr
rename KingmakersElectors KingmakersElectors_10hr
rename Elite_Ethnicity Elite_Ethnicity_10hr
rename Polity Atlas_10hr

rename Atlas_1880_6hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_1880_6hr
rename Elite_Structure LineageStruc_1880_6hr
rename N_Sources N_Sources_1880_6hr
rename Trade_FoodStuffs_Livestock Trade_Food_Livestock_1880_6hr
rename Trade_Minerals Trade_Minerals_1880_6hr
rename Trade_EnslavedPersons Trade_EnslavedPersons_1880_6hr
rename Economy_Agriculture Economy_Agriculture_1880_6hr
rename Economy_Pastoralism Economy_Pastoralism_1880_6hr
rename Economy_Trade Economy_Trade_1880_6hr
rename Caste Caste_1880_6hr
rename Slavery Slavery_1880_6hr
rename Polity_Age Polity_Age_1880_6hr
rename Persistence Persistence_1880_6hr
rename Stability Stability_1880_6hr
rename Trade_Rtes Trade_Rtes_1880_6hr
rename Polity Atlas_1880_6hr

rename Atlas_1880_8hr Polity
replace Polity = "Kgatla" if Polity == "BaKgatla"
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_1880_8hr
rename Elite_Structure LineageStruc_1880_8hr
rename N_Sources N_Sources_1880_8hr
rename Trade_FoodStuffs_Livestock Trade_Food_Livestock_1880_8hr
rename Trade_Minerals Trade_Minerals_1880_8hr
rename Trade_EnslavedPersons Trade_EnslavedPersons_1880_8hr
rename Economy_Agriculture Economy_Agriculture_1880_8hr
rename Economy_Pastoralism Economy_Pastoralism_1880_8hr
rename Economy_Trade Economy_Trade_1880_8hr
rename Caste Caste_1880_8hr
rename Slavery Slavery_1880_8hr
rename Polity_Age Polity_Age_1880_8hr
rename Persistence Persistence_1880_8hr
rename Stability Stability_1880_8hr
rename Trade_Rtes Trade_Rtes_1880_8hr
rename Polity Atlas_1880_8hr

rename Atlas_1880_10hr Polity
replace Polity = "Kgatla" if Polity == "BaKgatla"
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_1880_10hr
rename Elite_Structure LineageStruc_1880_10hr
rename N_Sources N_Sources_1880_10hr
rename Trade_FoodStuffs_Livestock Trade_Food_Livestock_1880_10hr
rename Trade_Minerals Trade_Minerals_1880_10hr
rename Trade_EnslavedPersons Trade_EnslavedPersons_1880_10hr
rename Economy_Agriculture Economy_Agriculture_1880_10hr
rename Economy_Pastoralism Economy_Pastoralism_1880_10hr
rename Economy_Trade Economy_Trade_1880_10hr
rename Caste Caste_1880_10hr
rename Slavery Slavery_1880_10hr
rename Polity_Age Polity_Age_1880_10hr
rename Persistence Persistence_1880_10hr
rename Stability Stability_1880_10hr
rename Trade_Rtes Trade_Rtes_1880_10hr
rename Polity Atlas_1880_10hr

rename Atlas_Composite_6hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_Composite_6hr
rename Elite_Structure LineageStruc_Composite_6hr
rename N_Sources N_Sources_Composite_6hr
rename Trade_FoodStuffs_Livestock Trade_Food_Livestock_Comp_6hr
rename Trade_Minerals Trade_Minerals_Composite_6hr
rename Trade_EnslavedPersons Trade_EnslavedPersons_Comp_6hr
rename Economy_Agriculture Economy_Agriculture_Comp_6hr
rename Economy_Pastoralism Economy_Pastoralism_Comp_6hr
rename Economy_Trade Economy_Trade_Comp_6hr
rename Caste Caste_Composite_6hr
rename Slavery Slavery_Composite_6hr
rename Polity_Age Polity_Age_Composite_6hr
rename Persistence Persistence_Composite_6hr
rename Stability Stability_Composite_6hr
rename Trade_Rtes Trade_Rtes_Composite_6hr
rename Polity Atlas_Composite_6hr

rename Atlas_Composite_8hr Polity
replace Polity = "Shawa" if Polity == "Shewa"
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_Composite_8hr
rename Elite_Structure LineageStruc_Composite_8hr
rename N_Sources N_Sources_Composite_8hr
rename Trade_FoodStuffs_Livestock Trade_Food_Livestock_Comp_8hr
rename Trade_Minerals Trade_Minerals_Composite_8hr
rename Trade_EnslavedPersons Trade_EnslavedPersons_Comp_8hr
rename Economy_Agriculture Economy_Agriculture_Comp_8hr
rename Economy_Pastoralism Economy_Pastoralism_Comp_8hr
rename Economy_Trade Economy_Trade_Comp_8hr
rename Caste Caste_Composite_8hr
rename Slavery Slavery_Composite_8hr
rename Polity_Age Polity_Age_Composite_8hr
rename Persistence Persistence_Composite_8hr
rename Stability Stability_Composite_8hr
rename Trade_Rtes Trade_Rtes_Composite_8hr
rename Polity Atlas_Composite_8hr

rename Atlas_Composite_10hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_Composite_10hr
rename Elite_Structure LineageStruc_Composite_10hr
rename N_Sources N_Sources_Composite_10hr
rename Trade_FoodStuffs_Livestock Trade_Food_Livestock_Comp_10hr
rename Trade_Minerals Trade_Minerals_Composite_10hr
rename Trade_EnslavedPersons Trade_EnslavedPersons_Comp_10hr
rename Economy_Agriculture Economy_Agriculture_Comp_10hr
rename Economy_Pastoralism Economy_Pastoralism_Comp_10hr
rename Economy_Trade Economy_Trade_Comp_10hr
rename Caste Caste_Composite_10hr
rename Slavery Slavery_Composite_10hr
rename Polity_Age Polity_Age_Composite_10hr
rename Persistence Persistence_Composite_10hr
rename Stability Stability_Composite_10hr
rename Trade_Rtes Trade_Rtes_Composite_10hr
rename Polity Atlas_Composite_10hr

rename Atlas_Composite_1880_6hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_Composite_1880_6hr
rename Elite_Structure LineageStruc_Composite_1880_6hr
rename N_Sources N_Sources_Composite_1880_6hr
rename Trade_FoodStuffs_Livestock Trade_Food_Live_Comp_1880_6hr
rename Trade_Minerals Trade_Minerals_Comp_1880_6hr
rename Trade_EnslavedPersons Trade_Enslaved_Comp_1880_6hr
rename Economy_Agriculture Economy_Agr_Comp_1880_6hr
rename Economy_Pastoralism Economy_Pastoral_Comp_1880_6hr
rename Economy_Trade Economy_Trade_Comp_1880_6hr
rename Caste Caste_Composite_1880_6hr
rename Slavery Slavery_Composite_1880_6hr
rename Polity_Age Polity_Age_Composite_1880_6hr
rename Persistence Persistence_Composite_1880_6hr
rename Stability Stability_Composite_1880_6hr
rename Trade_Rtes Trade_Rtes_Composite_1880_6hr
rename Polity Atlas_Composite_1880_6hr

rename Atlas_Composite_1880_8hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_Composite_1880_8hr
rename Elite_Structure LineageStruc_Composite_1880_8hr
rename N_Sources N_Sources_Composite_1880_8hr
rename Trade_FoodStuffs_Livestock Trade_Food_Live_Comp_1880_8hr
rename Trade_Minerals Trade_Minerals_Comp_1880_8hr
rename Trade_EnslavedPersons Trade_Enslaved_Comp_1880_8hr
rename Economy_Agriculture Economy_Agr_Comp_1880_8hr
rename Economy_Pastoralism Economy_Pastoral_Comp_1880_8hr
rename Economy_Trade Economy_Trade_Comp_1880_8hr
rename Caste Caste_Composite_1880_8hr
rename Slavery Slavery_Composite_1880_8hr
rename Polity_Age Polity_Age_Composite_1880_8hr
rename Persistence Persistence_Composite_1880_8hr
rename Stability Stability_Composite_1880_8hr
rename Trade_Rtes Trade_Rtes_Composite_1880_8hr
rename Polity Atlas_Composite_1880_8hr

rename Atlas_Composite_1880_10hr Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_Composite_1880_10hr
rename Elite_Structure LineageStruc_Composite_1880_10hr
rename N_Sources N_Sources_Composite_1880_10hr
rename Trade_FoodStuffs_Livestock Trade_Food_Live_Comp_1880_10hr
rename Trade_Minerals Trade_Minerals_Comp_1880_10hr
rename Trade_EnslavedPersons Trade_Enslaved_Comp_1880_10hr
rename Economy_Agriculture Economy_Agr_Comp_1880_10hr
rename Economy_Pastoralism Economy_Pastoral_Comp_1880_10hr
rename Economy_Trade Economy_Trade_Comp_1880_10hr
rename Caste Caste_Composite_1880_10hr
rename Slavery Slavery_Composite_1880_10hr
rename Polity_Age Polity_Age_Composite_1880_10hr
rename Persistence Persistence_Composite_1880_10hr
rename Stability Stability_Composite_1880_10hr
rename Trade_Rtes Trade_Rtes_Composite_1880_10hr
rename Polity Atlas_Composite_1880_10hr

rename Final_polity_uncertaintymin Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_uncertaintyMIN
rename Elite_Structure LineageStruc_uncertaintyMIN
rename N_Sources N_Sources_uncertaintyMIN
rename Trade_FoodStuffs_Livestock Trade_Food_Live_uncertaintyMIN
rename Trade_Minerals Trade_Minerals_uncertaintyMIN
rename Trade_EnslavedPersons Trade_Enslaved_uncertaintyMIN
rename Economy_Agriculture Economy_Agr_uncertaintyMIN
rename Economy_Pastoralism Economy_Pastoral_uncertaintyMIN
rename Economy_Trade Economy_Trade_uncertaintyMIN
rename Caste Caste_uncertaintyMIN
rename Slavery Slavery_uncertaintyMIN
rename Polity_Age Polity_Age_uncertaintyMIN
rename Persistence Persistence_uncertaintyMIN
rename Stability Stability_uncertaintyMIN
rename Trade_Rtes Trade_Rtes_uncertaintyMIN
rename Elite_Ethnicity Elite_Ethnicity_uncertaintyMIN
rename Polity Atlas_uncertaintyMIN

rename Final_polity_uncertaintymax Polity
sort Polity
merge Polity using "Data/Calculated Output/Atlas_Final.dta"
drop _merge
rename Ideology Ideology_uncertaintyMAX
rename Elite_Structure LineageStruc_uncertaintyMAX
rename N_Sources N_Sources_uncertaintyMAX
rename Trade_FoodStuffs_Livestock Trade_Food_Live_uncertaintyMAX
rename Trade_Minerals Trade_Minerals_uncertaintyMAX
rename Trade_EnslavedPersons Trade_Enslaved_uncertaintyMAX
rename Economy_Agriculture Economy_Agr_uncertaintyMAX
rename Economy_Pastoralism Economy_Pastoral_uncertaintyMAX
rename Economy_Trade Economy_Trade_uncertaintyMAX
rename Caste Caste_uncertaintyMAX
rename Slavery Slavery_uncertaintyMAX
rename Polity_Age Polity_Age_uncertaintyMAX
rename Persistence Persistence_uncertaintyMAX
rename Stability Stability_uncertaintyMAX
rename Trade_Rtes Trade_Rtes_uncertaintyMAX
rename Polity Atlas_uncertaintyMAX
sort grid_id
merge grid_id using "Data/Calculated Output/GED_GRID_Final.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/Acled_GRID_Final.dta"
drop _merge
sort grid_id
merge grid_id using "Data/Calculated Output/MetaWealth_GRID_Final.dta"
drop _merge

gen ln_d_coast_km = log(D_Coast_km) 
gen ln_mean2013 = ln(nightlight_20132_mean)
gen ln_dist_capital_km = ln(dist_capital_km)
gen ln_dist_capital_km_pre2011 = ln(dist_capital_km_pre2011)

gen Centralized_6hr = 0
replace Centralized_6hr = 1 if Atlas_6hr != ""
gen Centralized_8hr = 0
replace Centralized_8hr = 1 if Atlas_8hr != ""
gen Centralized_10hr = 0
replace Centralized_10hr = 1 if Atlas_10hr != ""

gen Polity_Type_6hr = 0
replace Polity_Type_6hr = 1 if LineageStruc_6hr == "Federation"
replace Polity_Type_6hr = 2 if LineageStruc_6hr == "Gatekeeper"
replace Polity_Type_6hr = 2 if LineageStruc_6hr == "gatekeeper"
replace Polity_Type_6hr = 3 if LineageStruc_6hr == "Regal"
replace Polity_Type_6hr = 4 if LineageStruc_6hr == "Despotic"

gen Polity_Type_8hr = 0
replace Polity_Type_8hr = 1 if LineageStruc_8hr == "Federation"
replace Polity_Type_8hr = 2 if LineageStruc_8hr == "Gatekeeper"
replace Polity_Type_8hr = 2 if LineageStruc_8hr == "gatekeeper"
replace Polity_Type_8hr = 3 if LineageStruc_8hr == "Regal"
replace Polity_Type_8hr = 4 if LineageStruc_8hr == "Despotic"

gen Polity_Type_10hr = 0
replace Polity_Type_10hr = 1 if LineageStruc_10hr == "Federation"
replace Polity_Type_10hr = 2 if LineageStruc_10hr == "Gatekeeper"
replace Polity_Type_10hr = 2 if LineageStruc_10hr == "gatekeeper"
replace Polity_Type_10hr = 3 if LineageStruc_10hr == "Regal"
replace Polity_Type_10hr = 4 if LineageStruc_10hr == "Despotic"

gen Centralized_1880_6hr = 0
replace Centralized_1880_6hr = 1 if Atlas_1880_6hr != ""
gen Centralized_1880_8hr = 0
replace Centralized_1880_8hr = 1 if Atlas_1880_8hr != ""
gen Centralized_1880_10hr = 0
replace Centralized_1880_10hr = 1 if Atlas_1880_10hr != ""

gen Polity_Type_1880_8hr = 0
replace Polity_Type_1880_8hr = 1 if LineageStruc_1880_8hr == "Federation"
replace Polity_Type_1880_8hr = 2 if LineageStruc_1880_8hr == "Gatekeeper"
replace Polity_Type_1880_8hr = 2 if LineageStruc_1880_8hr == "gatekeeper"
replace Polity_Type_1880_8hr = 3 if LineageStruc_1880_8hr == "Regal"
replace Polity_Type_1880_8hr = 4 if LineageStruc_1880_8hr == "Despotic"

gen Centralized_Comp_1880_8hr = 0
replace Centralized_Comp_1880_8hr = 1 if Atlas_Composite_1880_8hr != ""

gen Polity_Type_Comp_8hr = 0
replace Polity_Type_Comp_8hr = 1 if LineageStruc_Composite_8hr == "Federation"
replace Polity_Type_Comp_8hr = 2 if LineageStruc_Composite_8hr == "Gatekeeper"
replace Polity_Type_Comp_8hr = 2 if LineageStruc_Composite_8hr == "gatekeeper"
replace Polity_Type_Comp_8hr = 3 if LineageStruc_Composite_8hr == "Regal"
replace Polity_Type_Comp_8hr = 4 if LineageStruc_Composite_8hr == "Despotic"
replace Polity_Type_Comp_8hr = 0 if Atlas_Composite_1880_8hr == ""
replace Centralized_Comp_1880_8hr = 0 if Polity_Type_Comp_8hr == 0

gen Polity_Type_Comp_8hr_Fed = 0
replace Polity_Type_Comp_8hr_Fed = 1 if Polity_Type_Comp_8hr == 1
gen Polity_Type_Comp_8hr_Gate = 0
replace Polity_Type_Comp_8hr_Gate = 1 if Polity_Type_Comp_8hr == 2
gen Polity_Type_Comp_8hr_Regal = 0
replace Polity_Type_Comp_8hr_Regal = 1 if Polity_Type_Comp_8hr == 3
gen Polity_Type_Comp_8hr_Desp = 0
replace Polity_Type_Comp_8hr_Desp = 1 if Polity_Type_Comp_8hr == 4

gen Centralized_uncertaintyMAX = 0
replace Centralized_uncertaintyMAX = 1 if Atlas_uncertaintyMAX != ""
gen Centralized_uncertaintyMIN = 0
replace Centralized_uncertaintyMIN = 1 if Atlas_uncertaintyMIN != ""

gen Polity_Type_MAX = 0
replace Polity_Type_MAX = 1 if LineageStruc_uncertaintyMAX == "Federation"
replace Polity_Type_MAX = 2 if LineageStruc_uncertaintyMAX == "Gatekeeper"
replace Polity_Type_MAX = 2 if LineageStruc_uncertaintyMAX == "gatekeeper"
replace Polity_Type_MAX = 3 if LineageStruc_uncertaintyMAX == "Regal"
replace Polity_Type_MAX = 4 if LineageStruc_uncertaintyMAX == "Despotic"
replace Polity_Type_MAX = 0 if Atlas_uncertaintyMAX == ""

gen Polity_Type_MIN = 0
replace Polity_Type_MIN = 1 if LineageStruc_uncertaintyMIN == "Federation"
replace Polity_Type_MIN = 2 if LineageStruc_uncertaintyMIN == "Gatekeeper"
replace Polity_Type_MIN = 2 if LineageStruc_uncertaintyMIN == "gatekeeper"
replace Polity_Type_MIN = 3 if LineageStruc_uncertaintyMIN == "Regal"
replace Polity_Type_MIN = 4 if LineageStruc_uncertaintyMIN == "Despotic"
replace Polity_Type_MIN = 0 if Atlas_uncertaintyMIN == ""

drop if mt_binary_mean == .

collapse (first) whiteeco_area-Polity_Type_MIN, by(grid_id)

tab Polity_Type_8hr, gen(Polity_Type)
rename Polity_Type1 Polity_Type_None
rename Polity_Type2 Polity_Type_Fed
rename Polity_Type3 Polity_Type_Gatekeeper
rename Polity_Type4 Polity_Type_Regal
rename Polity_Type5 Polity_Type_Despotic

gen NaturalResources_Dummy = 0
replace NaturalResources_Dummy = 1 if NaturalResources > 1

replace  LineageStruc_uncertaintyMAX = "Gatekeeper" if LineageStruc_uncertaintyMAX == "gatekeeper"
replace LineageStruc_uncertaintyMAX = "None" if Centralized_uncertaintyMAX == 0
replace  LineageStruc_uncertaintyMIN = "Gatekeeper" if LineageStruc_uncertaintyMIN == "gatekeeper"
replace LineageStruc_uncertaintyMIN = "None" if Centralized_uncertaintyMIN == 0

label define StateType 0 "None" 1 "Federation" 2 "Gatekeeper" 3 "Regal" 4 "Despotic"
label values Polity_Type_MAX StateType

gen Polity_Type_MAX_Fed = 0
replace Polity_Type_MAX_Fed = 1 if Polity_Type_MAX == 1
gen Polity_Type_MAX_Gate = 0
replace Polity_Type_MAX_Gate = 1 if Polity_Type_MAX == 2
gen Polity_Type_MAX_Regal = 0
replace Polity_Type_MAX_Regal = 1 if Polity_Type_MAX == 3
gen Polity_Type_MAX_Desp = 0
replace Polity_Type_MAX_Desp = 1 if Polity_Type_MAX == 4

label values Polity_Type_MIN StateType

gen Polity_Type_MIN_Fed = 0
replace Polity_Type_MIN_Fed = 1 if Polity_Type_MIN == 1
gen Polity_Type_MIN_Gate = 0
replace Polity_Type_MIN_Gate = 1 if Polity_Type_MIN == 2
gen Polity_Type_MIN_Regal = 0
replace Polity_Type_MIN_Regal = 1 if Polity_Type_MIN == 3
gen Polity_Type_MIN_Desp = 0
replace Polity_Type_MIN_Desp = 1 if Polity_Type_MIN == 4

gen Polity_Type_1880_8hr_Fed = 0
replace Polity_Type_1880_8hr_Fed = 1 if Polity_Type_1880_8hr == 1
gen Polity_Type_1880_8hr_Gate = 0
replace Polity_Type_1880_8hr_Gate = 1 if Polity_Type_1880_8hr == 2
gen Polity_Type_1880_8hr_Regal = 0
replace Polity_Type_1880_8hr_Regal = 1 if Polity_Type_1880_8hr == 3
gen Polity_Type_1880_8hr_Desp = 0
replace Polity_Type_1880_8hr_Desp = 1 if Polity_Type_1880_8hr == 4

gen Acephalous_8hr = 0
replace Acephalous_8hr = 1 if Centralized_8hr == 0

gen grid_latitude2 = grid_latitude*grid_latitude
gen grid_longitude2 = grid_longitude*grid_longitude

gen lat_long = grid_longitude*grid_latitude

encode country, gen(Country)

hhi whiteeco_area, by(Atlas_8hr)
rename hhi_whiteeco_area Eco_Diversity_8hr
replace Eco_Diversity_8hr = . if whiteeco_area == .

gen Polity_Age_8hr2 = 0
replace Polity_Age_8hr2 = 1 if Polity_Age_8hr == "0_49yrs"
replace Polity_Age_8hr2 = 2 if Polity_Age_8hr == "50_99yrs"
replace Polity_Age_8hr2 = 3 if Polity_Age_8hr == "100_150yrs"
replace Polity_Age_8hr2 = 3 if Polity_Age_8hr == "150_200yrs"
replace Polity_Age_8hr2 = 4 if Polity_Age_8hr == "200_250yrs"
replace Polity_Age_8hr2 = 4 if Polity_Age_8hr == "250_299yrs"
replace Polity_Age_8hr2 = 5 if Polity_Age_8hr == "300yrsPlus"
label define age 1 "0-49 yrs" 2 "50-99 yrs" 3 "100-199 yrs" 4  "200-299 yrs" 5 "+300 yrs"
label values Polity_Age_8hr2 age 

gen UK = 0
replace UK = 1 if Colonizer == "UK"

sort grid_id
merge grid_id using  "Data/Calculated Output/Polity_Country_Area.dta"
drop _merge

egen tag = tag(country Atlas_8hr)
egen N_Polities = total(tag), by(country)

gen Atlas_8hr2 = Atlas_8hr
replace Atlas_8hr2 = "" if Polity_Perc_Nation_8hr < 0.02

egen tag2 = tag(country Atlas_8hr2)
egen N_Polities_no_rump = total(tag2), by(country)

gen Polity_size_8hr = .
replace Polity_size_8hr = 1 if Polity_Perc_Nation_8hr < .101 & Polity_Perc_Nation_8hr != .
replace Polity_size_8hr = 2 if Polity_Perc_Nation_8hr > .1 & Polity_Perc_Nation_8hr < 0.301 & Polity_Perc_Nation_8hr != .
replace Polity_size_8hr = 3 if Polity_Perc_Nation_8hr > .30 & Polity_Perc_Nation_8hr != .

replace Polity_8hr_ttl_area_km = . if Atlas_8hr == ""

replace EWI_Index = EWI_Index/100

drop if grid_perc_h20 >99

gen Min_State_Dist1966 = min(natcap1966, regcap1966)
gen Min_State_Dist1968 = min(natcap1968, regcap1968)
gen Min_State_Dist1975 = min(natcap1975, regcap1975)
gen Min_State_Dist1977 = min(natcap1977, regcap1977)
gen Min_State_Dist1980 = min(natcap1980, regcap1980)
gen Min_State_Dist2000 = min(natcap2000, regcap2000)
gen Min_State_Dist2015 = min(natcap2015, regcap2015)

gen Min_D_State_Indep = Min_State_Dist1966
replace Min_D_State_Indep = Min_State_Dist1968 if country == "Equatorial Guinea" | country == "Swaziland"
replace Min_D_State_Indep = Min_State_Dist1975 if country == "Angola" | country == "Mozambique"
replace Min_D_State_Indep = Min_State_Dist1977 if country == "Djibouti"
replace Min_D_State_Indep = Min_State_Dist1980 if country == "Zimbabwe"

sort grid_id
merge grid_id using "Data/Covariates_precipitation.dta"
drop _merge
replace popdensity_0ad = . if grid_longitude == .
* picking up hanish islands = Yemen
replace popdensity_0ad = . if grid_id == 148522 | grid_id == 148983 | grid_id == 149442 | grid_id == 149443 
gen ln_popdensity_0ad = log(0.001+popdensity_0ad)

sum CHIRPS_Prec_mm  mt_binary_mean grid_perc_h20 elevation Rugged_RuggednessFinal grid_longitude grid_latitude D_Coast_km landsuitability  
sum CHIRPS_Prec_mm  mt_binary_mean grid_perc_h20 elevation Rugged_RuggednessFinal grid_longitude grid_latitude D_Coast_km landsuitability  if Centralized_8hr == 1

gen Counterfactual = 1 
replace Counterfactual = 0 if elevation <  -4.711936
replace Counterfactual = 0 if elevation > 3802.465
replace Counterfactual = 0 if Rugged_RuggednessFinal > 13.62398
replace Counterfactual = 0 if grid_longitude < -17.30101
replace Counterfactual = 0 if grid_latitude < -30.70789
replace Counterfactual = 0 if grid_latitude > 19.69211
replace Counterfactual = 0 if CHIRPS_Prec_mm >  3581.288
replace Counterfactual = 0 if CHIRPS_Prec_mm <  4.198814
replace Counterfactual = 0 if D_Coast_km <  .0374297

collapse (first) whiteeco_area-Counterfactual, by(grid_id)

tab Polity_Type_8hr, gen (Type_)
label var Type_2 "Federation"
label var Type_3 "Gatekeeper"
label var Type_4 "Regal"
label var Type_5 "Despotic"  

tab Polity_Type_MAX, gen (Type_Max_)
label var Type_Max_2 "Federation"
label var Type_Max_3 "Gatekeeper"
label var Type_Max_4 "Regal"
label var Type_Max_5 "Despotic"  

tab Polity_Type_MIN, gen (Type_Min_)
label var Type_Min_2 "Federation"
label var Type_Min_3 "Gatekeeper"
label var Type_Min_4 "Regal"
label var Type_Min_5 "Despotic" 

gen ln_CHIRPS_Prec_mm = log(0.001+CHIRPS_Prec_mm)
label var ln_CHIRPS_Prec_mm "ln Prec (mm)"

sort grid_id
save "Data/APSR_Data_Final.dta", replace
}


